| Conditions | 4 |
| Total Lines | 65 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 2 | * Creates a new queue. A queue is a first-in-first-out (FIFO) data structure - |
||
| 3 | * items are added to the end of the queue and removed from the front. |
||
| 4 | * @example |
||
| 5 | * // create a new queue |
||
| 6 | * var queue = new Queue(); |
||
| 7 | * // enqueue an item |
||
| 8 | * queue.enqueue('item'); |
||
| 9 | * // dequeue an item |
||
| 10 | * var item = queue.dequeue(); |
||
| 11 | * // get the item at the front of the queue |
||
| 12 | * var item = queue.peek(); |
||
| 13 | * // determine the number of items in the queue |
||
| 14 | * var length = queue.getLength(); |
||
| 15 | * // determine whether the queue is empty |
||
| 16 | * var isEmpty = queue.isEmpty(); |
||
| 17 | */ |
||
| 18 | function Queue() { |
||
| 19 | // initialise the queue and offset |
||
| 20 | var queue = []; |
||
| 21 | var offset = 0; |
||
| 22 | |||
| 23 | // Returns the length of the queue. |
||
| 24 | this.getLength = function () { |
||
| 25 | return queue.length - offset; |
||
| 26 | }; |
||
| 27 | |||
| 28 | // Returns true if the queue is empty, and false otherwise. |
||
| 29 | this.isEmpty = function () { |
||
| 30 | return queue.length == 0; |
||
| 31 | }; |
||
| 32 | |||
| 33 | /* Enqueues the specified item. The parameter is: |
||
| 34 | * |
||
| 35 | * item - the item to enqueue |
||
| 36 | */ |
||
| 37 | this.enqueue = function (item) { |
||
| 38 | queue.push(item); |
||
| 39 | }; |
||
| 40 | |||
| 41 | /* Dequeues an item and returns it. If the queue is empty, the value |
||
| 42 | * 'undefined' is returned. |
||
| 43 | */ |
||
| 44 | this.dequeue = function () { |
||
| 45 | // if the queue is empty, return immediately |
||
| 46 | if (queue.length == 0) return undefined; |
||
| 47 | |||
| 48 | // store the item at the front of the queue |
||
| 49 | var item = queue[offset]; |
||
| 50 | |||
| 51 | // increment the offset and remove the free space if necessary |
||
| 52 | if (++offset * 2 >= queue.length) { |
||
| 53 | queue = queue.slice(offset); |
||
| 54 | offset = 0; |
||
| 55 | } |
||
| 56 | |||
| 57 | // return the dequeued item |
||
| 58 | return item; |
||
| 59 | }; |
||
| 60 | |||
| 61 | /* Returns the item at the front of the queue (without dequeuing it). If the |
||
| 62 | * queue is empty then undefined is returned. |
||
| 63 | */ |
||
| 64 | this.peek = function () { |
||
| 65 | return queue.length > 0 ? queue[offset] : undefined; |
||
| 66 | }; |
||
| 69 |